package funs

import (
	"os"
	"strings"
)

func patht(p, p2 string) string {
	if len(p2) == 0 {
		return p
	}
	p2 = strings.TrimPrefix(p2, "./")
	p2 = strings.TrimPrefix(p2, "/")
	if strings.HasSuffix(p, "/") {
		return p + p2
	}
	return p + "/" + p2
}

func Path(paths ...string) (ps string) {
	for i := range paths {
		p := paths[i]
		if i == 0 {
			ps = p
		} else {
			ps = patht(ps, p)
		}
	}
	return
}

func IsFileYml(path, table string) (string, error) {
	ymlPath := patht(path, table+".yml")
	_, err := os.Stat(ymlPath)
	if err != nil {
		ymlPath = patht(path, table+".yaml")
		_, err = os.Stat(ymlPath)
		if err != nil {
			return "", err
		}
		return "", err
	}
	return ymlPath, nil
}
